package com.codeZ.StrategyPattern.strategy;

import com.codeZ.StrategyPattern.normal.Receipt;
import com.codeZ.StrategyPattern.normal.ReceiptBuilder;

import java.util.List;

/**
 * 客户端
 *
 * @author codeZhang
 * @Date 2024/8/28 09:32
 */
public class Client {


    public static void main(String[] args) {

        //模拟回执
        List<Receipt> receiptList = ReceiptBuilder.getReceiptsList();


        //策略上下文
        ReceiptStrategyContext context = new ReceiptStrategyContext();
        //获取置策略
        ReceiptHandleStrategyFactory.init();
        //策略模式将策略的 定义、创建、使用这三部分进行了解耦
        for (Receipt receipt : receiptList) {
            ReceiptHandleStrategy strategy = ReceiptHandleStrategyFactory.getReceiptHandleStrategy(receipt.getType());
            if(strategy == null){
                System.out.println("回执类型："+receipt.getType()+"，未找到对应的策略");
                continue;
            }
            //设置策略
            context.setReceiptHandleStrategy(strategy);
            //执行策略
            context.handleReceipt(receipt);
        }
    }

}
